package cn.kimming.wms.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.kimming.wms.dao.ISaleAccountDAO;
import cn.kimming.wms.dao.IStockOutcomeBillDAO;
import cn.kimming.wms.domain.SaleAccount;
import cn.kimming.wms.domain.StockOutcomeBill;
import cn.kimming.wms.domain.StockOutcomeBillItem;
import cn.kimming.wms.query.QueryObject;
import cn.kimming.wms.service.IProductStockService;
import cn.kimming.wms.service.IStockOutcomeBillService;
import cn.kimming.wms.util.OrderCodeFactory;
import cn.kimming.wms.util.UserContext;

@Service("stockOutcomeBillService")
public class StockOutcomeBillServiceImpl implements IStockOutcomeBillService {

	@Autowired
	private IStockOutcomeBillDAO stockOutcomeBillDAO;
	@Autowired
	private IProductStockService productStockService;
	@Autowired
	private ISaleAccountDAO saleAccountDAO;
	
	@Override
	public void save(StockOutcomeBill bill) {
		//生成销售出库订单编号
		bill.setSn(OrderCodeFactory.getOutcomeCode(UserContext.getUser().getId()));
		//设置制单人和制单时间
		bill.setInputUser(UserContext.getUser());
		bill.setInputTime(new Date());
		//重新设置订单的未审核状态
		bill.setStatus(StockOutcomeBill.NORMAL);
		handleRelation(bill);
		//保存订单
		stockOutcomeBillDAO.save(bill);
	}
	
	//抽取方法:专门用来处理订单和明细之间的关系
	private void handleRelation(StockOutcomeBill bill) {
		//初始化订单基本信息, 处理订单和明细之间的关系
		bill.setTotalNumber(BigDecimal.ZERO);
		bill.setTotalAmount(BigDecimal.ZERO);
		for (StockOutcomeBillItem item : bill.getItems()) {
			//处理订单与明细之间的关系
			item.setBill(bill);
			//计算明细小计
			item.setAmount(item.getSalePrice().multiply(item.getNumber()).setScale(2, RoundingMode.HALF_UP));
			//计算订单总数量和总金额
			bill.setTotalNumber(bill.getTotalNumber().add(item.getNumber()));
			bill.setTotalAmount(bill.getTotalAmount().add(item.getAmount()));
		}
	}
	
	@Override
	public void update(StockOutcomeBill bill) {
		if (bill.getStatus() == StockOutcomeBill.NORMAL) {
			handleRelation(bill);
			stockOutcomeBillDAO.update(bill);
		}
	}

	@Override
	public void delete(Long id) {
		stockOutcomeBillDAO.delete(stockOutcomeBillDAO.get(id));
		
	}

	@Override
	public StockOutcomeBill get(Long id) {
		return stockOutcomeBillDAO.get(id);
	}

	@Override
	public List<StockOutcomeBill> list() {
		return stockOutcomeBillDAO.list();
	}

	@Override
	public List<StockOutcomeBill> query(QueryObject qo) {
		return stockOutcomeBillDAO.query(qo);
	}

	@Override
	public void audit(Long billId) {
		StockOutcomeBill bill = stockOutcomeBillDAO.get(billId);
		if (bill == null) {
			throw new NullPointerException("审核失败, 无效订单!");
		}
		if (bill.getStatus() == StockOutcomeBill.NORMAL) {
			bill.setStatus(StockOutcomeBill.AUDIT);
			bill.setAuditor(UserContext.getUser());
			bill.setAuditTime(new Date());
			//操作库存
			for (StockOutcomeBillItem item : bill.getItems()) {
				BigDecimal costPrice = productStockService.stockOutcome(bill.getDepot(), item);
				//生成每个明细对应销售账
				SaleAccount sa = new SaleAccount();
				sa.setVdate(bill.getVdate());
				sa.setNumber(item.getNumber());
				sa.setCostPrice(costPrice);
				sa.setCostAmount(costPrice.multiply(item.getNumber()).setScale(2, RoundingMode.HALF_UP));
				sa.setSalePrice(item.getSalePrice());
				sa.setSaleAmount(item.getAmount());
				sa.setClient(bill.getClient());
				sa.setProduct(item.getProduct());
				sa.setSaleman(bill.getInputUser());
				saleAccountDAO.save(sa);
			}
			stockOutcomeBillDAO.update(bill);
		}
	}
}
